//
//  MemberWelfareSecondGoodsView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/30.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "MemberWelfareSecondGoodsView.h"
#import "MemberWelfareSecondGoodsCollectionViewCell.h"
#import "PopupWebView.h"
#import <AudioToolbox/AudioToolbox.h>
static NSString *goodsCollectionViewCellID = @"goodsCollectionViewCellID";

@interface MemberWelfareSecondGoodsView () <UICollectionViewDelegate, UICollectionViewDataSource>
{
    SystemSoundID scoreClickBtnID;   // 自己设置的音效 (系统的振动音效 - kSystemSoundID_Vibrate)
}
@property (strong, nonatomic) UICollectionView *goodsCollectionView;
@property (nonatomic, strong) NSArray *dataarray;
@end

@implementation MemberWelfareSecondGoodsView


- (instancetype)init {
    
    if (self = [super init]) {
        
//        self.backgroundColor = Color1f89ff;
        //        self.goodsCollectionView.backgroundColor = Colorff0000;
        
        [self addSubview:self.goodsCollectionView];
        
        __weak typeof(self) weakSelf = self;
        
        [self.goodsCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(weakSelf.mas_left).with.offset(jisuan(40));
            make.right.mas_equalTo(weakSelf.mas_right);
            make.top.mas_equalTo(weakSelf.mas_top).with.offset(jisuan(20));
            make.bottom.mas_equalTo(weakSelf.mas_bottom);
            
        }];
         AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"5a2e388b2886c.wav" ofType:@""]]), &scoreClickBtnID);
    }
    
    return self;
}

#pragma mark -delegate \ datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataarray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//
    MemberWelfareSecondGoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:goodsCollectionViewCellID forIndexPath:indexPath];
    cell.goodListModel = self.dataarray[indexPath.row];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PopupWebView *popppview = [[PopupWebView alloc] init];
    popppview.hidden = YES;
    popppview.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [[UIApplication sharedApplication].keyWindow addSubview:popppview];
    
    SupportCategoryGoodListModel *model = self.dataarray[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"{\"user_id\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey]];
    [PPNetworkHelper POST:@"welfare/browse/index" parameters:@{@"json":string} success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            [popppview loadWithHtml:responseObject[@"data"][@"jiangli"]];
            popppview.hidden = NO;
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
            AudioServicesPlaySystemSound(self->scoreClickBtnID);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                [popppview removeFromSuperview];
                //                [weakSelf.navigationController pushViewController:ctr animated:YES];
                NSDictionary *dict = @{@"goodId":model.goodId};
                [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSHTOGOODDETAIL" object:nil userInfo:dict];
            });
        } else {
            NSDictionary *dict = @{@"goodId":model.goodId};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSHTOGOODDETAIL" object:nil userInfo:dict];
        }
    } failure:^(NSError *error) {
        NSDictionary *dict = @{@"goodId":model.goodId};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSHTOGOODDETAIL" object:nil userInfo:dict];
    }];
}

- (void)reloadVVVDataArray:(NSArray *)array {
    self.dataarray = array;
}

#pragma mark -lazy
- (UICollectionView *)goodsCollectionView {
    
    if (!_goodsCollectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(jisuan(240), jisuan(320));
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = jisuan(40);

        _goodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _goodsCollectionView.backgroundColor = WhiteColor;
        
        _goodsCollectionView.bounces = NO;
        
        _goodsCollectionView.delegate = self;
        _goodsCollectionView.dataSource = self;
        
        _goodsCollectionView.showsVerticalScrollIndicator = NO;
        _goodsCollectionView.showsHorizontalScrollIndicator = NO;
        
        [_goodsCollectionView registerClass:[MemberWelfareSecondGoodsCollectionViewCell class] forCellWithReuseIdentifier:goodsCollectionViewCellID];
        
        if (@available(iOS 11.0, *)) {
            
            _goodsCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
    }
    
    return _goodsCollectionView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
